#coding=utf-8 
import sys  
reload(sys)  
sys.setdefaultencoding('utf-8') 
import unittest
import logging
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from ec.util import commonUtil
from ec.util import globle


class OrderTest(unittest.TestCase):

	browser = None

	def __init__(self, methodName,browser):
		self.browser = browser
		unittest.TestCase.__init__(self, methodName)
	##初始化工作 
	def setUp(self): 
		self.browser.get(globle.WEB_URL)
		#打开首页，如果未登录则执行登录
		try:
			WebDriverWait(self.browser, 5).until(lambda x: "/portal/products" in self.browser.current_url)
		except:
			username = self.browser.find_element_by_name("username")
			username.clear()
			username.send_keys("zxzbszd")
			password = self.browser.find_element_by_name("password")
			password.clear()
			password.send_keys("123456")
			submitBtn = self.browser.find_element_by_class_name("btn-submit")
			submitBtn.send_keys(Keys.RETURN)


	def testPayOrder(self):
		
		#判断登录是否完成
		try:
			WebDriverWait(self.browser, 5).until(lambda x: "/portal/products" in self.browser.current_url)
		except:
			commonUtil.saveErrorImage(self.browser,r"report\errorImage\order\testPayOrder.png")
			self.assertTrue(False,"testPayOrder 未成功登录<errorImage\order\testPayOrder.png>")

		#搜索框进行商品搜索
		self.browser.find_element_by_class_name("header-search-input").send_keys("6162 7161 029")
		self.browser.find_element_by_class_name("header-search-submit").click()

		#立即购买搜索到的第一个商品
		try:
			trolleyBtn = WebDriverWait(self.browser, 5).until(lambda x: self.browser.find_element_by_class_name("btn-to-trolley"))
		except:
			commonUtil.saveErrorImage(self.browser,r"report\errorImage\order\testPayOrder.png")
			self.assertTrue(False,"testPayOrder 未搜索到商品<errorImage\order\testPayOrder.png>")
		trolleyBtn.click()

		#判断是否成功跳转购物车
		try:
			WebDriverWait(self.browser, 5).until(lambda x: "/orders/ShoppingCart" in self.browser.current_url)
		except:
			commonUtil.saveErrorImage(self.browser,r"report\errorImage\order\testPayOrder.png")
			self.assertTrue(False,"testPayOrder 未成功跳转到购物车<errorImage\order\testPayOrder.png>")

		#结算购物车
		self.browser.find_element_by_id("c_go").click()
		try:
			WebDriverWait(self.browser, 5).until(lambda x: "/orders/CheckoutOptions" in self.browser.current_url)
		except:
			commonUtil.saveErrorImage(self.browser,r"report\errorImage\order\testPayOrder.png")
			self.assertTrue(False,"testPayOrder 未成功跳转到结算页<errorImage\order\testPayOrder.png>")

		#下订单
		divs = self.browser.find_elements_by_id("logistic-company")
		for div in divs:
			div.find_element_by_tag_name("button").click()
			lis = div.find_element_by_tag_name("ul").find_elements_by_tag_name("li")
			for li in lis:
				a = li.find_element_by_tag_name("a")
				if a.get_attribute("data-option") == "ZCPS":
					a.click()

		submitBtn = self.browser.find_element_by_id("orderSubmit").click()

